﻿using C1.Win.C1Ribbon;
using CWCS.BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.IO;
using CWCS.Common.Log;

namespace CWCS.UI
{
    public partial class FrmTaskEdit : C1RibbonForm
    {
        private string _tasktype;
        private string _taskno;
        private string _fromstation;
        private int _taskstatus = 0;
        private int _runstatus = 0;
        private string _barcode="";
        public static bool ischange = false;
        private TaskBLL dal = new TaskBLL();
        private LogHelper staManualLog = new LogHelper("staManualLog");
        private static byte[] KeysStr = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        public FrmTaskEdit()
        {
            InitializeComponent();
        }

        public FrmTaskEdit(string fromstation,string tasktype, string taskno,string barcode, string taskstatus, string runstatus)
        {
            InitializeComponent();
            _fromstation = fromstation;
            _tasktype = tasktype;
            _taskno = taskno;
            _barcode = barcode;
            switch (taskstatus)
            {
                case "未开始": _taskstatus = 0; break;
                case "已完成": _taskstatus = 1; break;
                case "执行中": _taskstatus = 2; break;
            }
            if (runstatus == "可执行")
                _runstatus = 1;
            else if (runstatus == "不可执行")
                _runstatus = 0;
        }
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要修改任务" + txt_taskno.Text + "吗?", "修改", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (dal.UpdateWCSTaskStatus(_fromstation, _taskno.ToString(), _barcode, _tasktype, cbb_taskstatus.SelectedIndex.ToString(), cbb_runstatus.SelectedIndex.ToString()))
                {
                    ischange = true;
                    staManualLog.Info("用户;" + BLLBase.user + "任务管理器修改：任务号" + _taskno.ToString() + "托盘条码：" + _barcode + "任务类型：" + _tasktype + "任务状态：" + cbb_taskstatus.SelectedIndex.ToString() + "执行状态：" + cbb_runstatus.SelectedIndex.ToString());
                }
                else
                {
                    MessageBox.Show("修改任务失败！");
                }
            }
            this.Close();
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmTaskEdit_Load(object sender, EventArgs e)
        {
            txt_taskno.Text = _taskno;
            txtBarCode.Text = _barcode;
            cbb_taskstatus.SelectedIndex = _taskstatus;
            cbb_runstatus.SelectedIndex = _runstatus;
        }
        public static string StringToMD5(string Password)
        {
            byte[] result = Encoding.Default.GetBytes(Password);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] output = md5.ComputeHash(result);
            return BitConverter.ToString(output).Replace("-", "");
        }

        /// <summary>
        /// DES加密字符串
        /// </summary>
        /// <param name="encryptString">待加密的字符串</param>
        /// <param name="encryptKey">加密密钥,要求为8位</param>
        /// <returns>加密成功返回加密后的字符串，失败返回源串</returns>
        public static string EncryptDES(string encryptString, string encryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] rgbIV = KeysStr;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            catch
            {
                return encryptString;
            }
        }
        /// <summary>
        /// DES解密字符串
        /// </summary>
        /// <param name="decryptString">待解密的字符串</param>
        /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
        /// <returns>解密成功返回解密后的字符串，失败返源串</returns>
        public static string DecryptDES(string decryptString, string decryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
                byte[] rgbIV = KeysStr;
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch
            {
                return decryptString;
            }
        }
    }
}
